import { ApiProperty } from '@nestjs/swagger'
import {
  IsBoolean,
  IsIn,
  IsInt,
  IsOptional,
  IsString,
  Min,
  MinLength,
  ValidateIf,
} from 'class-validator'
import { CommonStatus, MenuType } from '~/enums'

export class MenuSaveDto {
  @ApiProperty({ description: '菜单ID' })
  @IsInt()
  @Min(0)
  @IsOptional()
  id?: number

  @ApiProperty({
    description: `
菜单类型:
- 1: 目录
- 2: 菜单
- 3: 权限   
    `,
    enum: MenuType,
  })
  @IsIn([MenuType.MENU_GROUP, MenuType.MENU, MenuType.PERMISSION])
  type: MenuType

  @ApiProperty({ description: '父级菜单ID', default: 0 })
  @IsInt()
  @Min(0)
  @IsOptional()
  parentId?: number

  @ApiProperty({ description: '菜单或权限名称' })
  @IsString()
  @MinLength(2)
  name: string

  @ApiProperty({ description: '权限标识', default: '' })
  @IsString()
  @IsOptional()
  permission?: string

  @ApiProperty({ description: '显示顺序', default: 0 })
  @IsInt()
  @Min(0)
  @IsOptional()
  sort?: number

  @ApiProperty({ description: '前端路由地址', default: '' })
  @ValidateIf(o => o.type !== MenuType.PERMISSION)
  @IsString()
  @IsOptional()
  path?: string

  @ApiProperty({ description: '菜单图标', default: '#' })
  @IsOptional()
  @ValidateIf((o: MenuSaveDto) => o.type !== MenuType.PERMISSION)
  @IsString()
  icon?: string

  @ApiProperty({ description: '组件路径' })
  @ValidateIf((o: MenuSaveDto) => o.type !== MenuType.PERMISSION)
  @IsString()
  @IsOptional()
  component?: string

  @ApiProperty({ description: '组件名' })
  @ValidateIf((o: MenuSaveDto) => o.type !== MenuType.PERMISSION)
  @IsString()
  @IsOptional()
  componentName?: string

  @ApiProperty({ description: '菜单状态', default: 0 })
  @IsIn([CommonStatus.NORMAL, CommonStatus.DISABLED])
  @IsOptional()
  status?: CommonStatus

  @ApiProperty({ description: '是否可见', default: true })
  @IsBoolean()
  @IsOptional()
  visible?: boolean

  @ApiProperty({ description: '是否缓存', default: true })
  @IsBoolean()
  @IsOptional()
  keepAlive?: boolean

  @ApiProperty({ description: '是否总是显示', default: true })
  @IsBoolean()
  @IsOptional()
  alwaysShow?: boolean
}

export class MenuQueryDto {
  @ApiProperty({ description: '菜单类型' })
  @IsIn([MenuType.MENU_GROUP, MenuType.MENU, MenuType.PERMISSION])
  @IsOptional()
  type?: MenuType

  @ApiProperty({ description: '父级菜单ID' })
  @IsInt()
  @Min(0)
  @IsOptional()
  parentId?: number

  @ApiProperty({ description: '菜单或权限名称' })
  @IsString()
  @IsOptional()
  name?: string

  @ApiProperty({ description: '权限标识' })
  @IsString()
  @IsOptional()
  permission?: string

  @ApiProperty({ description: '前端路由地址' })
  @IsString()
  @IsOptional()
  path?: string

  @ApiProperty({ description: '菜单图标' })
  @IsString()
  @IsOptional()
  icon?: string

  @ApiProperty({ description: '组件路径' })
  @IsString()
  @IsOptional()
  component?: string

  @ApiProperty({ description: '组件名' })
  @IsString()
  @IsOptional()
  componentName?: string

  @ApiProperty({ description: '菜单状态' })
  @IsIn([CommonStatus.NORMAL, CommonStatus.DISABLED])
  @IsOptional()
  status?: CommonStatus

  @ApiProperty({ description: '是否可见' })
  @IsBoolean()
  @IsOptional()
  visible?: boolean

  @ApiProperty({ description: '是否缓存' })
  @IsBoolean()
  @IsOptional()
  keepAlive?: boolean

  @ApiProperty({ description: '是否总是显示' })
  @IsBoolean()
  @IsOptional()
  alwaysShow?: boolean
}
